package top.datacluster.basic.algorithm.leetcode;

public class CircularLinkedLeetcode141 {

    public boolean hasCycle(ListNode head) {
        if (head == null){
            return true;
        }

        if (head.next == null){
            return true;
        }
        ListNode first = head;
        ListNode second = head.next;
        while (first != second){
            if (second.next == null || second.next.next == null){
                return false;
            }
            first = first.next;
            second = second.next.next;
        }
        return true;
    }


    public static class ListNode{
        int val;

        ListNode next;

        ListNode(int x) { val = x; }
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(0);
        ListNode node4 = new ListNode(4);
//        ListNode node5 = new ListNode(5);
        node1.next = node2;
//        node2.next = node3;
//        node3.next = node4;
//        node4.next = node2;
        System.out.println(new CircularLinkedLeetcode141().hasCycle(node1));

    }

}
